package flc.ast.fragment;

import android.Manifest;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CameraActivity;
import flc.ast.activity.RecCameraActivity;
import flc.ast.adapter.DocumentScanAdapter;
import flc.ast.databinding.FragmentScanBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ScanFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ScanFragment extends BaseNoModelFragment<FragmentScanBinding> {

    private DocumentScanAdapter documentScanAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_scan;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivAnimalRec.setOnClickListener(this);
        mDataBinding.ivPlantRec.setOnClickListener(this);

        mDataBinding.rvDocumentList.setLayoutManager(new GridLayoutManager(mContext, 4));
        documentScanAdapter = new DocumentScanAdapter();
        mDataBinding.rvDocumentList.setAdapter(documentScanAdapter);
        documentScanAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivAnimalRec) {//动物识别
            getPermission(1, 1);
        } else if (id == R.id.ivPlantRec) {//植物识别
            getPermission(0, 1);
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        getPermission(position, 0);
    }

    @Override
    protected void initData() {
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.acsz);
        list.add(R.drawable.ahkb);
        list.add(R.drawable.asfz);
        list.add(R.drawable.ahz);
        list.add(R.drawable.ayyzz);
        list.add(R.drawable.ajsz);
        list.add(R.drawable.axsz);
        list.add(R.drawable.ajszz);
        list.add(R.drawable.ajhz);
        list.add(R.drawable.alhz);
        documentScanAdapter.setList(list);
    }

    private void getPermission(int position, int flag) {
        UserSysEventProxy.getInstance().payEventWithFreeNumOfTimes(getActivity(), Content.fragment_scan_key + position + flag,  Content.fragment_scan_max,
                isRealOk -> {
                    if (isRealOk) {
                        StkPermissionHelper.permission(Manifest.permission.CAMERA).reqPermissionDesc(getString(R.string.get_camera_permission)).callback(new StkPermissionHelper.ACallback() {
                            @Override
                            public void onGranted() {
                                if (flag == 0) {
                                    CameraActivity.type = position;
                                    startActivity(CameraActivity.class);
                                } else {
                                    RecCameraActivity.type = position;
                                    startActivity(RecCameraActivity.class);
                                }
                            }
                        }).request();
                    }
                });
    }

}